package com.orientation.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.orientation.commom.exception.CustomException;
import com.orientation.dto.BulletinDto;
import com.orientation.entity.Bulletin;
import com.orientation.mapper.BulletinMapper;
import com.orientation.service.BulletinService;
import com.orientation.service.StudentService;
import com.orientation.utils.BeanConvertUtils;
import com.orientation.utils.PageUtils;
import com.orientation.vo.BulletinStudentVo;
import com.orientation.vo.StudentClassInfoVo;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xuandian
 * @since 2022-02-12
 */
@Service
public class BulletinServiceImpl extends ServiceImpl<BulletinMapper, Bulletin> implements BulletinService {
    @Resource
    private StudentService studentService;

    @Override
    public void saveBatchBulletin(BulletinDto bulletinDto) {
        if (!ObjectUtils.isEmpty(bulletinDto) && !CollectionUtils.isEmpty(bulletinDto.getStudentIds()) && !ObjectUtils.isEmpty(bulletinDto.getBulletin()) && StringUtils.hasLength(bulletinDto.getBulletin().getContent())) {
            List<Bulletin> updateOrUpdateBuilder = new ArrayList<>();
            for (int i = 0; i < bulletinDto.getStudentIds().size(); i++) {
                Bulletin bulletin = new Bulletin();
                BeanUtils.copyProperties(bulletinDto.getBulletin(), bulletin);
                bulletin.setStudentId(bulletinDto.getStudentIds().get(i));
                Bulletin bulletinOne = this.getOne(Wrappers.<Bulletin>lambdaQuery().eq(Bulletin::getStudentId, bulletin.getStudentId())
                        .eq(Bulletin::getTitle, bulletin.getTitle()));
                if (!ObjectUtils.isEmpty(bulletinOne)) {
                    bulletin.setSid(bulletinOne.getSid());
                }
                updateOrUpdateBuilder.add(bulletin);
            }
            if (!CollectionUtils.isEmpty(updateOrUpdateBuilder)) {
                this.saveOrUpdateBatch(updateOrUpdateBuilder);
            }
        } else {
            throw new CustomException("18", "必要信息没有添加");
        }
    }

    @Override
    public List<BulletinStudentVo> bulletinStudentVo(String majorId, String className, String studentId, String adviserId, String createTime) {
        List<StudentClassInfoVo> studentClassInfoVos = studentService.getStudentClassInfoVos(null, majorId, className, null, null, "1", studentId);
        if (CollectionUtils.isEmpty(studentClassInfoVos)) {
            throw new CustomException("18", "暂无信息");
        }
        // List<StudentClassInfoVo> insertPile = studentClassInfoVos.stream().filter(m -> !reserves.stream().map(Reserve::getStudentId).collect(toList()).contains(m.getStudentId())).collect(toList());

        List<StudentClassInfoVo> studentClassInfoVoList = studentClassInfoVos.stream().filter((StudentClassInfoVo s) -> s.getAdviserId().equals(adviserId)).collect(toList());
        if (CollectionUtils.isEmpty(studentClassInfoVoList)) {
            throw new CustomException("18", "只能查看自己所在专业学生信息");
        }
        List<String> studentIds = studentClassInfoVoList.stream().map(StudentClassInfoVo::getStudentId).distinct().collect(toList());
        List<Bulletin> bulletins = this.list(Wrappers.<Bulletin>lambdaQuery().in(Bulletin::getStudentId, studentIds).likeRight(StringUtils.hasLength(createTime), Bulletin::getCreateTime, createTime));
        //
        List<StudentClassInfoVo> voList = studentClassInfoVos.stream().filter(m -> bulletins.stream().map(Bulletin::getStudentId).collect(toList()).contains(m.getStudentId())).collect(toList());
        List<BulletinStudentVo> bulletinStudentVos = BeanConvertUtils.copyListProperties(bulletins, BulletinStudentVo::new);
        for (BulletinStudentVo bulletinStudentVo : bulletinStudentVos) {
            for (StudentClassInfoVo studentClassInfoVo : voList) {
                if (studentClassInfoVo.getStudentId().equals(bulletinStudentVo.getStudentId())) {
                    bulletinStudentVo.setCollegeName(studentClassInfoVo.getCollegeName());
                    bulletinStudentVo.setMajorName(studentClassInfoVo.getMajorName());
                    bulletinStudentVo.setClassName(studentClassInfoVo.getClassName());
                    bulletinStudentVo.setStudentName(studentClassInfoVo.getStudentName());
                }
            }
        }
        if (!CollectionUtils.isEmpty(bulletinStudentVos)) {
            bulletinStudentVos = bulletinStudentVos.stream().sorted(Comparator.comparing(BulletinStudentVo::getStudentId)).collect(Collectors.toList());
        }
        return bulletinStudentVos;
    }

    @Override
    public IPage<BulletinStudentVo> queryBulletinStudent(Integer pageNum, Integer pageSize, String majorId, String className, String studentId, String adviserId, String createTime) {
        List<BulletinStudentVo> bulletinStudentVos = bulletinStudentVo(majorId, className, studentId, adviserId, createTime);
        return PageUtils.getPage(bulletinStudentVos, pageNum, pageSize);
    }
}
